home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 12 / Cream of the Crop 12 (Part II) / Cream of the Crop 12 (Part II).iso / OS2 / GNUSUTIL.ZIP / lib / error.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-12-17  |  3.1 KB  |  131 lines

  1. /* error.c -- error handler for noninteractive utilities
  2.    Copyright (C) 1990, 91, 92, 93, 94 Free Software Foundation, Inc.
  3.  
  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 2, or (at your option)
  7.    any later version.
  8.  
  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; if not, write to the Free Software
  16.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. /* Written by David MacKenzie <djm@gnu.ai.mit.edu>.  */
  19.  
  20. #ifdef HAVE_CONFIG_H
  21. #include <config.h>
  22. #endif
  23.  
  24. #include <stdio.h>
  25.  
  26. #if HAVE_VPRINTF || HAVE_DOPRNT
  27. # if __STDC__
  28. #  include <stdarg.h>
  29. #  define VA_START(args, lastarg) va_start(args, lastarg)
  30. # else
  31. #  include <varargs.h>
  32. #  define VA_START(args, lastarg) va_start(args)
  33. # endif
  34. #else
  35. # define va_alist a1, a2, a3, a4, a5, a6, a7, a8
  36. # define va_dcl char *a1, *a2, *a3, *a4, *a5, *a6, *a7, *a8;
  37. #endif
  38.  
  39. #if STDC_HEADERS
  40. # include <stdlib.h>
  41. # include <string.h>
  42. #else
  43. void exit ();
  44. #endif
  45.  
  46. /* If NULL, error will flush stdout, then print on stderr the program
  47.    name, a colon and a space.  Otherwise, error will call this
  48.    function without parameters instead.  */
  49. void (*error_print_progname) () = NULL;
  50.  
  51. /* The calling program should define program_name and set it to the
  52.    name of the executing program.  */
  53. extern char *program_name;
  54.  
  55. #if HAVE_STRERROR
  56. char *strerror ();
  57. #else
  58. static char *
  59. private_strerror (errnum)
  60.      int errnum;
  61. {
  62.   extern char *sys_errlist[];
  63.   extern int sys_nerr;
  64.  
  65.   if (errnum > 0 && errnum <= sys_nerr)
  66.     return sys_errlist[errnum];
  67.   return "Unknown system error";
  68. }
  69. #define strerror private_strerror
  70. #endif
  71.  
  72. #ifdef __EMX__
  73.  
  74. char *program_name;
  75.  
  76. void set_program_name(char *name)
  77. {
  78.   program_name = name;
  79. }
  80.  
  81. #endif
  82.  
  83. /* Print the program name and error message MESSAGE, which is a printf-style
  84.    format string with optional args.
  85.    If ERRNUM is nonzero, print its corresponding system error message.
  86.    Exit with status STATUS if it is nonzero.  */
  87. /* VARARGS */
  88.  
  89. void
  90. #if defined(VA_START) && __STDC__
  91. error (int status, int errnum, const char *message, ...)
  92. #else
  93. error (status, errnum, message, va_alist)
  94.      int status;
  95.      int errnum;
  96.      char *message;
  97.      va_dcl
  98. #endif
  99. {
  100. #ifdef VA_START
  101.   va_list args;
  102. #endif
  103.  
  104.   if (error_print_progname)
  105.     (*error_print_progname) ();
  106.   else
  107.     {
  108.       fflush (stdout);
  109.       fprintf (stderr, "%s: ", program_name);
  110.     }
  111.  
  112. #ifdef VA_START
  113.   VA_START (args, message);
  114. # if HAVE_VPRINTF
  115.   vfprintf (stderr, message, args);
  116. # else
  117.   _doprnt (message, args, stderr);
  118. # endif
  119.   va_end (args);
  120. #else
  121.   fprintf (stderr, message, a1, a2, a3, a4, a5, a6, a7, a8);
  122. #endif
  123.  
  124.   if (errnum)
  125.     fprintf (stderr, ": %s", strerror (errnum));
  126.   putc ('\n', stderr);
  127.   fflush (stderr);
  128.   if (status)
  129.     exit (status);
  130. }
  131.